﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Services;

public partial class adm_vlg_faqs : System.Web.UI.Page
{
    CommonPage cm3 = new CommonPage();
    VLGDataContext _db8 = new VLGDataContext();
    //string cateId = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        if (IsPostBack) return;
        //LoadListCategory();
        fillDulieuLenGrid();
    }


    void fillDulieuLenGrid()
    {

        var list = from a in _db8.FAQs
                   orderby a.DateQuestion descending
                   select a;
        GridView1.DataSource = list;
        GridView1.DataBind();
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="idAcc"></param>
    /// <returns>
    /// 0 : có lỗi
    /// 1 : trả ve text là đang hoạt động
    /// 2 : trả ve đã khóa
    /// </returns>
    /// Van chua kiem tra admin moi duoc thao tac
    [WebMethod]
    public static int AdminActiveAcc(string idAcc)
    {

        if (!string.IsNullOrEmpty(idAcc))
        {
            try
            {
                VLGDataContext _dataContext = new VLGDataContext();
                CommonPage _comment = new CommonPage();

                var acc = (from o in _dataContext.VLGAccounts
                           where o.idAcc == Convert.ToInt32(idAcc) //&& _comment.Customer.idGroup.ToLower().Equals("admin")
                           select o).Single();
                if (acc.AdminDisableAcc == true)
                {
                    acc.AdminDisableAcc = false;
                    acc.DateAdminDisableAcc = DateTime.Now;
                    _dataContext.SubmitChanges();
                    return 1; //chuyen text thành đang hoạt động
                }
                else
                {
                    acc.AdminDisableAcc = true;
                    acc.DateAdminDisableAcc = DateTime.Now;
                    _dataContext.SubmitChanges();
                    return 2; //chuyen text thành đã khóa
                }
            }
            catch
            {

                return 0;
            }

        }
        else return 0;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        fillDulieuLenGrid();
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            FAQ faq = e.Row.DataItem as FAQ;
            // Display the company name in italics.
            //e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
            HyperLink hp = e.Row.FindControl("hpAnHien") as HyperLink;
            HyperLink hpDelete = e.Row.FindControl("hpDelete") as HyperLink;
            if (faq != null)
            {
                //xu ly khoa mo tai khoan
                if (faq.IsActive == true)
                {
                    hp.Text = "Hiện";
                    hp.ToolTip = "Click để ẩn hiện nội dung";
                    hp.NavigateUrl = faq.idFAQ.ToString();
                    hp.CssClass = "active_faq";
                }
                else
                {
                    hp.Text = "Ẩn ";
                    hp.ToolTip = "Click để ẩn hiện nội dung";
                    hp.NavigateUrl = faq.idFAQ.ToString();
                    hp.CssClass = "active_faq";
                }
                hpDelete.NavigateUrl = faq.idFAQ.ToString();
                hpDelete.Text ="Xóa";
                hpDelete.ToolTip = "Click để xóa faq";
            }

        }

    }
    //Cập nhật ẩn hiện trên lưới
    public void chkAnHien_OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkAnHien = (CheckBox)sender;
        GridViewRow row = (GridViewRow)chkAnHien.NamingContainer;
        string cid = chkAnHien.ToolTip;
        bool status = chkAnHien.Checked;
        Category cate = _db8.Categories.SingleOrDefault(x => x.idCategory == int.Parse(cid));
        cate.AnHien = status;
        _db8.SubmitChanges();
        // fillDulieuLenGrid();
    }
    protected void linkAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveClientUrl("~/qlcongviec.aspx?op=add"));
    }

    protected void linkEditG_Click(object sender, EventArgs e)
    {

    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="idAcc"></param>
    /// <returns>
    /// 0 : có lỗi
    /// 1 : trả ve text là đang hoạt động
    /// 2 : trả ve đã khóa
    /// </returns>
    /// Van chua kiem tra admin moi duoc thao tac
    [WebMethod]
    public static int AdminActiveFaq(string idAcc)
    {

        if (!string.IsNullOrEmpty(idAcc))
        {
            try
            {
                VLGDataContext _dataContext = new VLGDataContext();
                CommonPage _comment = new CommonPage();

                var acc = (from o in _dataContext.FAQs
                           where o.idFAQ == Convert.ToInt32(idAcc) //&& _comment.Customer.idGroup.ToLower().Equals("admin")
                           select o).Single();
                if (acc.IsActive == true)
                {
                    acc.IsActive = false;
                    _dataContext.SubmitChanges();
                    return 1; //chuyen text thành đang hoạt động
                }
                else
                {
                    acc.IsActive = true;
                    _dataContext.SubmitChanges();
                    return 2; //chuyen text thành đã khóa
                }
            }
            catch
            {

                return 0;
            }

        }
        else return 0;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="idAcc"></param>
    /// <returns>
    /// 0 : có lỗi
    /// 1 : xóa thành công
    /// </returns>
    /// Van chua kiem tra admin moi duoc thao tac
    [WebMethod]
    public static int AdminDeleteFaq(string idAcc)
    {

        if (!string.IsNullOrEmpty(idAcc))
        {
            try
            {
                VLGDataContext _dataContext = new VLGDataContext();
                CommonPage _comment = new CommonPage();

                var acc = (from o in _dataContext.FAQs
                           where o.idFAQ == Convert.ToInt32(idAcc) //&& _comment.Customer.idGroup.ToLower().Equals("admin")
                           select o).Single();

                _dataContext.FAQs.DeleteOnSubmit(acc);
                _dataContext.SubmitChanges();
                return 1; //chuyen text thành đang hoạt động

            }
            catch
            {

                return 0;
            }

        }
        else return 0;
    }
}
